﻿using System.Collections.Generic;
using System.Data;
using POS.BusinessEntities;

namespace POS.DataObjects
{
    public class DOProveedor: DOBase
    {
        #region Declarations

        private const string getListSP = "ProveedorGetList";
        private const string saveSP = "ProveedorSave";
        
        #endregion

        #region Methods: Public

        public virtual IList<BEProveedor> GetList(int empresaId, string filter)
        {
            var result = new List<BEProveedor>();

            var command = Db.GetStoredProcCommand(getListSP);
            Db.AddInParameter(command, "@empresaId", DbType.Int32, empresaId);
            Db.AddInParameter(command, "@filtro", DbType.String, filter);
            var data = Db.ExecuteDataSet(command);

            if (data.Tables.Count == 1)
            {
                foreach (DataRow row in data.Tables[0].Rows)
                    result.Add(CreateEntityFromDataRow<BEProveedor>(row));
            }
            return result;
        }

        public virtual void Save(BEProveedor entity, int empresaId, string user)
        {
            var command = Db.GetStoredProcCommand(saveSP);
            Db.AddInParameter(command, "@id", DbType.Int32, entity.Id);
            Db.AddInParameter(command, "@nombre", DbType.String, entity.Nombre);
            Db.AddInParameter(command, "@empresaId", DbType.Int32, empresaId);
            Db.AddInParameter(command, "@cedula", DbType.String, entity.Cedula);
            Db.AddInParameter(command, "@telefono", DbType.String, entity.Telefono);
            Db.AddInParameter(command, "@email", DbType.String, entity.Email);
            Db.AddInParameter(command, "@paisId", DbType.Int32, entity.PaisId);
            Db.AddInParameter(command, "@provinciaId", DbType.Int32, entity.ProvinciaId);
            Db.AddInParameter(command, "@direccion", DbType.String, entity.Direccion);
            Db.AddInParameter(command, "@notas", DbType.String, entity.Notas);
            Db.AddInParameter(command, "@diasCredito", DbType.Int32, entity.DiasCredito);
            Db.AddInParameter(command, "@limiteCredito", DbType.Decimal, entity.LimiteCredito);
            Db.AddInParameter(command, "@tipoId", DbType.Int32, entity.TipoId);
            Db.AddInParameter(command, "@user", DbType.String, user);
            Db.ExecuteNonQuery(command);
        }
     
        #endregion
    }
}
